<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php
abstract class CApiComponent extends CComponent {
    private $_initialized = false;
    public $result = array();
    public $_userId;     public $_deviceId;     public $_parentId;     
    public $totalSpace;
    public $usedSpace;
    
    public function __construct() {
        set_exception_handler ( array ($this, 'handleException' ) );
    }
    
    
    public function init() {
                                set_exception_handler ( array ($this, 'handleException' ) );
        set_error_handler ( array ($this, 'handleError' ) );
        
        $this->_initialized = true;
    }
    
    
    public function handleError($code, $message, $file, $line) {
        return $this->displayError ( $code, $message, $file, $line );
    }
    
    
    public function handleException($exception) {
        $code = $exception->getCode ();
        if (empty ( $code )) {
            $code = 403;
        }
        header ( "HTTP/1.1 {$code} " . $exception->getMessage () );
        return $this->displayException ( $exception );
    }
    
    
    public function displayError($code, $message, $file, $line)
    {
        $content = "";
        if (YII_DEBUG)
        {
            $content = "<h1>PHP Error [$code]</h1>\n";
            $content .= "<p>$message ($file:$line)</p>\n";
            $content .= '<pre>';

                                                $trace = debug_backtrace();
            if (count($trace) > 3)
                $trace = array_slice($trace,3);

            foreach ($trace as $i=>$t)
            {
                if (!isset($t['file']))
                    $t['file']='unknown';
                if (!isset($t['line']))
                    $t['line']=0;
                if (!isset($t['function']))
                    $t['function']='unknown';
                $content .= "#$i {$t['file']}({$t['line']}): ";

                if (isset($t['object']) && is_object($t['object']))
                    $content .= get_class($t['object']).'->';
                $content .= "{$t['function']}()\n";
            }

            $content .= '</pre>';
        }
        else
        {
            $content = "<h1>PHP Error [$code]</h1>\n";
            $content .= "<p>$message</p>\n";
        }
        Yii::log($content, CLogger::LEVEL_ERROR);
    }
    
    
    public function displayException($exception)
    {
        $content = "";
        if (YII_DEBUG)
        {
            $content = '<h1>'.get_class($exception)."</h1>\n";
            $content .= '<p>'.$exception->getMessage();
            $content .= ' ('.$exception->getFile().':'.$exception->getLine().')</p>';
            $content .= '<pre>'.$exception->getTraceAsString().'</pre>';
        }
        else
        {
            $content = '<h1>'.get_class($exception)."</h1>\n";
            $content .= '<p>'.$exception->getMessage().'</p>';
        }
        Yii::log($content, CLogger::LEVEL_ERROR);
    }
    
    
    public function handleResult($status, $code, $message) {
        $this->result["state"]   = $status;
        $this->result["code"]    = $code;
        $this->result["msg"]     = $message;
    }
    
    
    public function handleEnd() {
        echo CJSON::encode ( $this->result );
        Yii::app ()->end ();
    }
    
    
    public function handleSpace() {
        $this->totalSpace =  CUtils::totalSize($this->_userId);
        $used  =  UserFile::model()->getUsedSpace($this->_userId);

        $this->usedSpace = 0;
        if (count($used) > 0){
            $this->usedSpace = $used[0]["usedSpace"];
        }
        if ($this->totalSpace <= $this->usedSpace) {
            $this->result["msg"]     = "空间不足";
            $this->result["message"] = "空间不足";
            throw new ApiException("File name is NULL.");
        }
    }
}
?>